package com.example.listcontainerdemo.slice;

import com.example.listcontainerdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        // 获取ListContainer实例
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
        // 创建一些模拟数据
        ArrayList<ItemModel> list = new ArrayList<>();
        for  (int i = 0; i < 1; i++) {
            // 新建ItemModel
            list.add(new ItemModel(String.format("第%d行数据", i)));
        }
        // 创建数据源类实例
        ItemProvider itemProvider = new ItemProvider(list, this.getContext());
        // 设置列表容器的数据源
        listContainer.setItemProvider(itemProvider);
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                if (i > 4) {
                    list.remove(i);
                } else {
                    list.add(new ItemModel("新增数据"));
                }
                itemProvider.notifyDataChanged();
            }
        });

        listContainer.setItemLongClickedListener(new ListContainer.ItemLongClickedListener() {
            @Override
            public boolean onItemLongClicked(ListContainer listContainer, Component component, int i, long l) {
                if (i > 0) {
                    list.remove(i);
                    itemProvider.notifyDataChanged();
                }
                return false;
            }
        });


    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
